<template>
  <div class="com-container">
    <div class="echarts" ref="ref_stock"></div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      echart: null,
      allData: [],
      currentIndex: 0, //当前显示的页数
      timeId: null,
    };
  },
  methods: {
    Initecharts() {
      this.echart = this.$echarts.init(this.$refs.ref_stock, "chalk");
      const option = {
        title: {
          text: "| 库存和销量分析",
          top: "20rem",
          left: "15rem",
          textStyle: {
            fontSize: "1rem",
          },
        },
      };
      this.echart.setOption(option);
      this.echart.on("mouseover", () => {
        clearInterval(this.timeId);
      });
      this.echart.on("mouseout", () => {
        this.setIntervalTime();
      });
    },
    async getDate() {
      const { data: res } = await this.$http.get("/stock");
      this.allData = res;
      this.updateEcharts();
      this.setIntervalTime();
    },
    //处理图标的问题
    updateEcharts() {
      const centerASrr = [
        ["18%", "40%"],
        ["50%", "40%"],
        ["82%", "40%"],
        ["34%", "75%"],
        ["66%", "75%"],
      ];
      const colorArr = [
        ["#4FF778", "#0BA82C"],
        ["#E5DD45", "#E8B11C"],
        ["#E8821C", "#E55445"],
        ["#5052EE", "#AB6EE5"],
        ["#23E5E5", "#2E72BF"],
      ];
      const seriesArr = [];
      // 计算位置
      const start = this.currentIndex * 5;
      const end = (this.currentIndex + 1) * 5;
      const showData = this.allData.slice(start, end);
      showData.forEach((item, index) => {
        seriesArr.push({
          type: "pie",
          radius: ["60rem", "50rem"],
          center: centerASrr[index],
          labelLine: { show: false },
          data: [
            {
              name: item.name + "\n\n" + item.sales,
              value: item.sales,
              label: {
                position: "center",
                fontSize: "1rem",
              },
              itemStyle: {
                color: {
                  type: "linear",
                  x: 0,
                  y: 1,
                  x2: 0,
                  y2: 0,
                  colorStops: [
                    {
                      offset: 0,
                      color: colorArr[index][0], // 0% 处的颜色
                    },
                    {
                      offset: 1,
                      color: colorArr[index][0], // 100% 处的颜色
                    },
                  ],
                  global: false, // 缺省为 false
                },
              },
            },
            {
              value: item.stock,
              itemStyle: {
                color: "#333843",
              },
            },
          ],
          emphasis: {
            scale: false, //关闭高亮是的缩放
          },
        });
      });
      const updatOption = {
        series: seriesArr,
      };
      this.echart.setOption(updatOption);
    },
    screenAdata() {
      const titleFontSize = (this.$refs.ref_stock.offsetWidth / 100) * 3.6;
      const outRadius = titleFontSize * 2.8;
      const inierRadius = titleFontSize * 1.125;
      const aa = {
        textStyle: {
          fontSize: titleFontSize,
        },
        series: {
          radius: [outRadius, inierRadius],
        },
      };
      this.echart.setOption(aa);
      this.echart.resize();
    },
    setIntervalTime() {
      this.timeId = setInterval(() => {
        this.currentIndex++;
        if (this.currentIndex > 1) {
          this.currentIndex = 0;
        }
        this.updateEcharts();
      }, 5000);
    },
  },
  created() {
    this.getDate();
  },
  mounted() {
    this.Initecharts();
    window.addEventListener("resize", this.screenAdata);
    this.screenAdata();
  },
  destroyed() {
    window.removeEventListener("resize", this.screenAdata);
    clearInterval(this.timeId);
  },
};
</script>

<style lang="less" scoped>
.com-container {
  width: 100%;
  height: 100%;
  overflow: hidden;
  margin: 0;
  padding: 0;

  .echarts {
    width: 100%;
    height: 100%;
    overflow: hidden;
    margin: 0;
    padding: 0;
  }
}
</style>